package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.client.po.ModulePo;
import com.msh.artascope.devcloud.client.po.ModuleResourcePo;
import com.msh.artascope.devcloud.client.qo.ModuleResourceQo;
import com.msh.artascope.devcloud.service.dao.ModuleResourceDao;
import com.msh.artascope.devcloud.service.model.query.ModuleResourceQueryVo;
import com.msh.artascope.devcloud.service.model.vo.ModuleResourceVo;
import com.msh.artascope.devcloud.service.service.KubernetesConfigService;
import com.msh.artascope.devcloud.service.service.ModuleResourceService;
import com.msh.artascope.devcloud.service.service.ModuleService;
import com.msh.artascope.devcloud.service.wrapper.K8sWrapper;
import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.interfaces.IdGenerateable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class ModuleResourceServiceImpl extends TenantBaseServiceImpl<ModuleResourcePo,ModuleResourceQo> implements ModuleResourceService {

  @Resource
  private ModuleResourceDao moduleResourceDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Resource
  private K8sWrapper k8sWrapper;

  @Resource
  private ModuleService moduleService;

  @Resource
  private KubernetesConfigService kubernetesConfigService;

  @Override
  public long insert(ModuleResourcePo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<ModuleResourcePo> param) {
    if(CollectionUtils.isEmpty(param)){
      return 0;
    }
    Long[] uniqueIds = idGenerateable.getUniqueIds(param.size());
    int i=0;
    for (ModuleResourcePo po : param) {
      po.setId(uniqueIds[i++]);
    }
    return super.insertCollection(param);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void update(ModuleResourcePo param) {
    boolean replicasChange = false;
    ModulePo modulePo = null;
    Long id = param.getId();
    Long tenantId = param.getTenantId();
    if(null != param.getReplicasNum()){
      ModuleResourcePo db = get(id, tenantId);
      if(!db.getReplicasNum().equals(param.getReplicasNum())){
        replicasChange = true;
        modulePo = moduleService.get(db.getModuleId(), tenantId);
      }
    }
    super.update(param);
  }


// ----------前端vo处理-------------------------------------------------------

  private ModuleResourceVo toVo(ModuleResourcePo po){
    ModuleResourceVo vo = po.convert(ModuleResourceVo.class);
    return vo;
  }

  private List<ModuleResourceVo> toVoList(List<ModuleResourcePo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public ModuleResourceVo getVo(Long id, Long tenantId) {
    ModuleResourcePo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    ModuleResourceVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<ModuleResourceVo> listVo(ModuleResourceQueryVo q){
    ModuleResourceQo qo = q.convert(ModuleResourceQo.class);
    List<ModuleResourcePo> poList = this.list(qo);
    List<ModuleResourceVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<ModuleResourceVo>> pageVo(ModuleResourceQueryVo param) {
    ModuleResourceQo qo = param.convert(ModuleResourceQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<ModuleResourceVo>emptyList()).setCount(count);
    }
    List<ModuleResourcePo> poList = this.list(qo);
    List<ModuleResourceVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


  @Override
  public ModuleResourceVo getVoByModuleId(Long moduleId, Long tenantId) {
    ModulePo modulePo = moduleService.get(moduleId, tenantId);
    BizException.doThrowIfNull("无法获取制品信息", modulePo);
    ModuleResourcePo moduleResourcePo = listOne(new ModuleResourceQo().setModuleId(moduleId).setTenantId(tenantId));
    BizException.doThrowIfNull("无法获取制品资源", moduleResourcePo);
    ModuleResourceVo convert = moduleResourcePo.convert(ModuleResourceVo.class);
    convert.setModuleType(modulePo.getType());
    return convert;
  }

}
